<?
include("includes/functions.php");

// Save poll
if (!empty($_GET[addSave])) {
	$pQ = fieldToDB($pQ);
	
	// Insert question into dbase
	$sql = "INSERT into ss_poll (
			PollQ,
			DateStart,
			DateEnd
			) values (
			'$pQ',
			'$dateStart',
			'$dateEnd'
			)";
	$result = mysql_db_query($dbname,$sql);	
	
	// Get poll Q ID
	$sql = "SELECT MAX(PID) from ss_poll";
	$result = mysql_db_query($dbname,$sql);
	$row = mysql_fetch_array($result);
	$pID = $row[0];
	
	// Now, enter possible answers
	for($a=0;$a<=7;$a++) {
		$pA = "pA_".$a;
		$pA = $$pA;
		
		if (!empty($pA)) {
			$pA = fieldToDB($pA);
			$sql = "INSERT into ss_poll (
					ParentID,
					PollA
					) values (
					'$pID',
					'$pA'
					)";
			$result = mysql_db_query($dbname,$sql);
		}
	}
}
if (!empty($_GET[editSave])) {
	$pID = $_POST[pID]; 
	$pQ  = fieldToDB($pQ);
	
	// Insert question into dbase
	$sql = "UPDATE ss_poll SET 
				PollQ = '$pQ',
				DateStart = '$dateStart',
				DateEnd   = '$dateEnd'
				WHERE PID = '$pID'";
	$result = mysql_db_query($dbname,$sql);	
	
	// Clear possible answers
	$sql = "DELETE from ss_poll WHERE ParentID = '$pID'";
	$result = mysql_db_query($dbname,$sql);	
	
	// Now, enter possible answers
	for($a=0;$a<=7;$a++) {
		$pA = "pA_".$a;
		$pA = $$pA;
		
		$pV = "pV_".$a;
		$pV = $$pV;
		
		if (!empty($pA)) {
			$pA = fieldToDB($pA);
			$sql = "INSERT into ss_poll (
					ParentID,
					PollA,
					Votes
					) values (
					'$pID',
					'$pA',
					'$pV'
					)";
			$result = mysql_db_query($dbname,$sql);
		}
	}		
	
	
}
if (!empty($_GET[edit])) {
	 // Retrieve data to populate form
	 $pID = $_GET[pID];
	 
	 // Get question
	 $sql = "SELECT * from ss_poll WHERE PID = '$pID'";
	 $result = mysql_db_query($dbname,$sql);
	 $row = mysql_fetch_assoc($result);
	 $pQ  = stripslashes($row[PollQ]);
	 $pDateStart = $row[DateStart];
	 $pDateEnd   = $row[DateEnd];
	 
	 // Get questions and votes
	 $sql = "SELECT * from ss_poll WHERE ParentID = '$pID' ORDER BY PID";
	 $result = mysql_db_query($dbname,$sql);
	 
	 $i=0;
	 while ($value = mysql_fetch_assoc($result)) {
	 	$pAArray[$i]    = stripslashes($value[PollA]);
	 	$pVoteArray[$i] = $value[Votes];
	 	$i++;
	 }
}

// Change active state
if ($_GET[changeActive] == 1) {
	$pID    = $_GET[pID];
	$active = $_GET[active]; 
	$sql = "UPDATE ss_poll SET Active = '$active' WHERE PID = '$pID'";
	$result = mysql_db_query($dbname,$sql);
}
// Change On Home state
if ($_GET[changeOnHome] == 1) {
	// Clear On Home state of all polls
	$sql = "UPDATE ss_poll SET OnHome = '0'";
	$result = mysql_db_query($dbname,$sql);
	
	$pID    = $_GET[pID];
	$onHome = $_GET[onHome]; 
	$sql = "UPDATE ss_poll SET OnHome = '$onHome' WHERE PID = '$pID'";
	$result = mysql_db_query($dbname,$sql);
}

// Delete poll
if ($_GET[delete] == 1) {
	$pID = $_GET[pID];
	$sql = "DELETE from ss_poll WHERE PID = '$pID' OR ParentID = '$pID'";
	$result = mysql_db_query($dbname,$sql);
}

// Get all current polls
$sql = "SELECT * from ss_poll WHERE ParentID = '0' ORDER BY DateStart";
$resultPoll = mysql_db_query($dbname,$sql);
$rows = mysql_num_rows($resultPoll);

include("includes/header.php"); ?>
<script language="JavaScript">
function confirmSubmit(text) {
	var agree=confirm(text);
	if (agree)
		return true ;
	else
		return false ;
}
</script>
<div id="header2">
	<? include("includes/main_menu.php"); ?>
</div>
<? include("includes/sub_menu_maint.php"); ?>
<div id="subNoLogin"></div>
<div id="pageHeader"><h1 class="headerUsers">Website Maintenance : <b>soniXshop Polls</b></h1></div>

<div id="pageInstructions"><img src="images/help.gif"> <br><br>

<?
if (!empty($message)) { echo "<font color=red>$message</font>\n"; }
?>
<p></p>
</div>

<div id="datatable">

<? if (!empty($_GET[add]) || !empty($_GET[edit])) { 
	if (!empty($_GET[add])) { $what = "addSave"; }
	else { $what = "editSave"; }
	if (empty($pDateStart)) { $pDateStart = date('Y-m-d'); }
	if (empty($pDateEnd))   { $pDateEnd   = date('Y-m-d'); }
?>
<form action="<?=$_SERVER[PHP_SELF]; ?>?<?=$what; ?>=1" method="POST">
<input type="hidden" name="pID" value="<?=$pID; ?>">
<table>
<tr>
	<td width="30%">Poll Publish Dates [Start, End]</td>
	<td width="70%" bgcolor="#eeeeee">
		<input type="text" name="dateStart" id=sel1 size="15" value="<?=$pDateStart; ?>" class="text" Readonly>
		<a href="#"><img src="images/cal.gif" width="16" height="16" border="0" alt="Click To Add Date" onclick="return showCalendar('sel1', 'y-mm-dd');"></a>
		<input type="text" name="dateEnd" id=sel2 size="15" value="<?=$pDateEnd; ?>" class="text" Readonly>
		<a href="#"><img src="images/cal.gif" width="16" height="16" border="0" alt="Click To Add Date" onclick="return showCalendar('sel2', 'y-mm-dd');"></a>
	</td>
</tr>
<tr>
	<td width="30%">Poll Question</td>
	<td width="70%" bgcolor="#eeeeee"><input type="text" value="<?=$pQ; ?>" name="pQ" size="50"></td>
</tr>
<tr>
	<td width="30%" valign="top">Possible Answers</td>
	<td width="70%" bgcolor="#eeeeee">
		<? for($a=0;$a<=7;$a++) { $b = $a+1; ?>
		<input type="text" value="<?=$pAArray[$a]; ?>" name="pA_<?=$a; ?>" size="50">
		<input type="hidden" name="pV_<?=$a; ?>" value="<?=$pVoteArray[$a]; ?>">
		<br>
		<? } ?>
	</td>
</tr>
<tr>
	<td width="30%"></td>
	<td width="70%" bgcolor="#eeeeee"><input type="image" src="images/save.gif" border="0" title="Save"></td>
</tr>
</table>
</form>
<? } ?>

<table class="datatable" summary="A List of Current Help Items">
	<caption>Current Polls : <a href="<?=$_SERVER[PHP_SELF]; ?>?add=1" title="Add New Poll"><b>Add new poll</b></a></caption>
	
	<? if ($rows > 0) { ?>
	<tr>
		<th width="8%" scope="col"></th>
		<th scope="col">Poll Question</th>
		<th scope="col">Date Range</th>
		<th scope="col">Votes</th>
		<th scope="col">Active</th>
		<th scope="col">On Home</th>
	</tr>
	<?
	$i = 0;
	while ($value = mysql_fetch_assoc($resultPoll)) {	
		if (($i % 2) > 0) { $class = "altrow"; $bgcolor = "#eeeeee"; }
		else { $class = null; $bgcolor = "#ffffff"; }
		
		$pID        = $value[PID];	
		$pQ         = stripslashes($value[PollQ]);
		$pDateStart = $value[DateStart];
		$pDateEnd   = $value[DateEnd];
		$pActive    = $value[Active];
		$pOnHome    = $value[OnHome];
		
		// Get total number of votes for all questions
		$sql = "SELECT SUM(Votes) from ss_poll WHERE PID = '$pID' OR ParentID = '$pID'";
		$result = mysql_db_query($dbname,$sql);
		$row = mysql_fetch_row($result);
		$totalVotes = $row[0];
  		
	?>
	<tr class="<?=$class; ?>" onmouseover="setPointer(this, '#E9FCB2')" onmouseout="setPointer(this, '<?=$bgcolor; ?>')">
	
		<td class="nav">
			<a href="<?=$_SERVER[PHP_SELF]; ?>?edit=1&pID=<?=$pID; ?>" title="Edit Poll"><img src="images/button_edit.gif" border="0"></a>
			<a href="<?=$_SERVER[PHP_SELF]; ?>?delete=1&pID=<?=$pID; ?>" title="Delete Poll" onClick="confirmSubmit('Are you sure you want to delete this poll?');"><img src="images/button_delete.gif" border="0"></a>
		</td>
		<td><?=$pQ; ?></td>
		<td><? mysql_to_date($pDateStart);
			echo " to ";
			mysql_to_date($pDateEnd); ?></td>
		<td><?=$totalVotes; ?></td>	
		<td>
			<?
			if ($pActive == 1) {
				echo "<a href=\"$_SERVER[PHP_SELF]?changeActive=1&active=0&pID=$pID\" title=\"Make Inactive\">Active</a>\n";
			} else {
				echo "<a href=\"$_SERVER[PHP_SELF]?changeActive=1&active=1&pID=$pID\" title=\"Make Active\"><font color=red>Inactive</font></a>\n";
			}
			?>
		</td>
		<td>
			<?
			if ($pOnHome == 1) {
				echo "<a href=\"$_SERVER[PHP_SELF]?changeOnHome=1&onHome=0&pID=$pID\" title=\"Remove from Home Page\">On Home</a>\n";
			} else {
				echo "<a href=\"$_SERVER[PHP_SELF]?changeOnHome=1&onHome=1&pID=$pID\" title=\"Put On Home Page\"><font color=#c0c0c0>On Home</font></a>\n";
			}
			?>
		</td>
	</tr>
	<?  $i++; }
		} else { ?>
	<tr><td>
	There are no polls set up at this time.<br>
	<a href="<?=$_SERVER[PHP_SELF]; ?>?add=1" title="Add New Poll"><img src="images/button_add.gif" border="0"></a>
	<a href="<?=$_SERVER[PHP_SELF]; ?>?add=1" title="Add New Poll">Add new Poll</a>
	
	</td></tr>
	<? } ?>
	
</table>
</div>		


<? include("includes/footer.php"); ?>